package com.ddwl.goods.service;

import cn.hutool.core.bean.BeanUtil;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.goods.dao.GoodsProfitDao;
import com.ddwl.goods.dao.model.GoodsProfit;
import com.ddwl.schema.bo.distr.SetGoodsProfitBo;
import com.ddwl.schema.bo.goods.QueryGoodsProfitBo;
import com.ddwl.schema.vo.dist.GoodsProfitVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 类GoodsProfitService.java的实现描述：商品分润配置
 *
 * @author zach
 * @Date :2020-05-25 14:19.
 */
@Service
public class ShopGoodsProfitService {

    @Resource
    private GoodsProfitDao goodsProfitDao;
    @Resource
    private CategoryService categoryService;
    @Resource
    private ItemService itemService;

    /**
     * 分润设置
     *
     * @param goodsProfitVo
     */
    public void setGoodsProfit(SetGoodsProfitBo goodsProfitVo) {
        verfiyGoodsProfit(goodsProfitVo);
        GoodsProfit goodsProfit = goodsProfitDao.getGoodsProfit(goodsProfitVo.getTargetId(), goodsProfitVo.getTargetType());
        if (goodsProfit != null) {
            BeanUtil.copyProperties(goodsProfitVo, goodsProfit);
            goodsProfitDao.updateByPrimaryKeySelective(goodsProfit);
        } else {
            goodsProfit = BeanConverUtils.convertBean(goodsProfitVo, GoodsProfit.class);
            goodsProfitDao.insert(goodsProfit);
        }
    }

    /**
     * 设置商品分润信息
     */
    public void setItemProfit(SetGoodsProfitBo bo, String itemId, String shopId) {
        if (bo != null) {
            bo.setShopId(shopId);
            bo.setTargetId(itemId);
            bo.setTargetType(GoodsEnum.ProfitTagetTypeEnum.SPU.getCode());
            setGoodsProfit(bo);
        } else {
            GoodsProfitVo vo = queryGoodsProfitVoByItemId(itemId, shopId);
            if (vo != null) {
                deleteGoodsProfit(vo.getId(), shopId);
            }
        }
    }

    /**
     * 删除分润配置
     *
     * @param id
     */
    public void deleteGoodsProfit(String id, String shopId) {
        ParamException.isBlank(id);
        GoodsProfit goodsProfit = goodsProfitDao.queryGoodsProfit(id, shopId);
        ParamException.isNull(goodsProfit);
        GoodsProfit goodsProfit1 = new GoodsProfit();
        goodsProfit1.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        goodsProfit1.setId(id);
        goodsProfitDao.updateByPrimaryKeySelective(goodsProfit1);
    }

    /**
     * 查询商品分润信息
     *
     * @param itemId
     * @return
     */
    public GoodsProfitVo queryGoodsProfitVoByItemId(String itemId, String shopId) {
        QueryGoodsProfitBo bo = new QueryGoodsProfitBo();
        bo.setTargetId(itemId);
        bo.setTargetType(GoodsEnum.ProfitTagetTypeEnum.SPU.getCode());
        bo.setShopId(shopId);
        List<GoodsProfitVo> goodsProfits = queryGoodsProfitVo(bo);
        if (!CollectionUtils.isEmpty(goodsProfits)) {
            return goodsProfits.get(0);
        }
        return null;
    }

    /**
     * 查询分润信息
     *
     * @param queryGoodsProfitVo
     * @return
     */
    public List<GoodsProfitVo> queryGoodsProfitVo(QueryGoodsProfitBo queryGoodsProfitVo) {
        return BeanConverUtils.convertBeanList(goodsProfitDao.queryGoodsProfits(queryGoodsProfitVo), GoodsProfitVo.class);
    }


    private void verfiyGoodsProfit(SetGoodsProfitBo goodsProfitVo) {
        ParamException.isNull(GoodsEnum.ProfitTagetTypeEnum.tagetTypeEnum(goodsProfitVo.getTargetType()));
        ParamException.isBlank(goodsProfitVo.getShopId(), "门店id不能为空");
        if (GoodsEnum.ProfitTagetTypeEnum.SPU.getCode().equals(goodsProfitVo.getTargetType())) {
            ParamException.isNull(itemService.getItemWithShop(goodsProfitVo.getTargetId(), goodsProfitVo.getShopId()));
        } else if (GoodsEnum.ProfitTagetTypeEnum.CATEGORY.getCode().equals(goodsProfitVo.getTargetType())) {
            ParamException.isNull(categoryService.getCateWithShopId(goodsProfitVo.getTargetId(), goodsProfitVo.getShopId()));
        } else if (GoodsEnum.ProfitTagetTypeEnum.PLATFORM.getCode().equals(goodsProfitVo.getTargetType())) {

        }
        ParamException.isNull(GoodsEnum.ProfitTypeEnum.tagetTypeEnum(goodsProfitVo.getProfitType()));
        if (GoodsEnum.ProfitTypeEnum.RATIO.getCode().equals(goodsProfitVo.getProfitType())) {
            ParamException.isFalse(goodsProfitVo.getRatio().compareTo(new BigDecimal("0.00")) >= 0 && goodsProfitVo.getRatio().compareTo(new BigDecimal("99.99")) <= 0, "佣金百分比不正确");
        } else {
            ParamException.isFalse(goodsProfitVo.getRatio().compareTo(new BigDecimal("0.00")) >= 0);
        }
    }

    public GoodsProfit selectOne(GoodsProfit goodsProfit) {
        if (null == goodsProfit) {
            return null;
        }
        return goodsProfitDao.selectOne(goodsProfit);
    }

}
